package demo.mapper;

import demo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {

    @Select("select *from userinfo")
    List<UserInfo> getUserInfoAll();

    //    @Select("select id, username, password, age, gender from userinfo where id = #{id}")
//    UserInfo getUserInfoById(Integer id);
    @Select("select id, username, password, age, gender from userinfo where id = #{userId}")
    UserInfo getUserInfoById(@Param("userId") Integer id);


    //增
//    @Insert("insert into userinfo (id, username, password, age, gender)" +
//            " value(#{id}, #{username}, #{password}, #{age}, #{gender})")
//    Integer insertUserInfo(UserInfo userInfo);
//    @Insert("insert into userinfo (id, username, password, age, gender)" +
//            " value(#{info.id}, #{info.username}, #{info.password}, #{info.age}, #{info.gender})")
//    Integer insertUserInfo(@Param("info") UserInfo userInfo);
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into userinfo (id, username, password, age, gender)" +
            " value(#{info.id}, #{info.username}, #{info.password}, #{info.age}, #{info.gender})")
    Integer insertUserInfo(@Param("info") UserInfo userInfo);

    //删
    @Delete("delete from userinfo where id = #{id}")
    Integer deleteUserInfoById(Integer id);

    //改
    @Update("update userinfo set username = #{username}, password = #{password}," +
            "age = #{age}, gender = #{gender} where id = #{id}")
    Integer updateUserInfo(UserInfo userInfo);

    //查
    @ResultMap("resultMap")   //映射复用
    @Select("select id, delete_flag, create_time,update_time from userinfo")
    List<UserInfo> getAll();

    @Select("select id, delete_flag as deleteFlag, create_time as createTime,update_time as updateTime from userinfo")
    List<UserInfo> getAll2();    //起别名

    @Select("select id, delete_flag, create_time,update_time from userinfo")
    @Results(id = "resultMap", value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    List<UserInfo> getAll3();    //结果映射

    //驼峰命名


//    @Select("select *from userinfo where username like '%#{key}%'")   //错误
    @Select("select *from userinfo where username like concat('%', #{key}, '%')")
    List<UserInfo> selectByLike(String key);

}